/**
 * @author harry
 * @date 2021/12/6/9:26
 */

package com.ui.contact;

import com.controller.ContactController;
import com.controller.GroupController;
import com.pojo.Contact;
import com.pojo.Group;
import com.ui.builder.ButtonBuilder;
import com.ui.builder.ComboboxBuilder;
import com.ui.builder.LabelBuilder;
import com.ui.builder.TextFieldBuilder;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class QueryContact extends JFrame implements ActionListener {
    private final JPanel inputPanel = new JPanel();
    private final JPanel buttonPanel = new JPanel();
    private final JLabel nameLabel = LabelBuilder.makeLabel("姓名");
    private final JLabel phoneLabel = LabelBuilder.makeLabel("电话");
    private final JLabel groupLabel = LabelBuilder.makeLabel("群组");
    private final JLabel queryLabel = LabelBuilder.makeLabel("查找方式");
    private final JTextField nameInput = TextFieldBuilder.makeTextField(15);
    private final JTextField phoneInput = TextFieldBuilder.makeTextField(15);
    private final JComboBox<Object> groupChoose = ComboboxBuilder.makeCombobox();
    private final JComboBox<Object> queryChoose = ComboboxBuilder.makeCombobox("0:按姓名查找", "1:按电话号码查找", "2:按分组查找", "3:按姓名和分组查找");
    private final JButton submit = ButtonBuilder.makeButton("确认");
    private final JButton reset = ButtonBuilder.makeButton("重置");
    private final ContactController contactController = new ContactController();
    private final GroupController groupController = new GroupController();
    private final JTable table;

    public QueryContact(JTable table) {
        super("查询联系人");
        this.table = table;

        submit.addActionListener(this);
        reset.addActionListener(this);

        List<Group> groupList = groupController.getAllGroups();
        if(groupList == null) {
            JOptionPane.showMessageDialog(null, "获取分组列表失败");
            return;
        }
        for(Group g: groupList) {
            groupChoose.addItem(g.getValue());
        }

        inputPanel.setLayout(new GridLayout(5, 2, 0, 10));
        inputPanel.add(queryLabel);
        inputPanel.add(queryChoose);
        inputPanel.add(nameLabel);
        inputPanel.add(nameInput);
        inputPanel.add(phoneLabel);
        inputPanel.add(phoneInput);
        inputPanel.add(groupLabel);
        inputPanel.add(groupChoose);
        inputPanel.add(new JLabel());
        inputPanel.add(new JLabel());
        buttonPanel.setLayout(new GridLayout(1, 2, 30, 0));
        buttonPanel.add(submit);
        buttonPanel.add(reset);

        this.setLayout(new FlowLayout());
        this.setSize(450, 300);
        this.setLocationRelativeTo(null);
        this.add(inputPanel);
        this.add(buttonPanel);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source == submit) {
            int queryMode = queryChoose.getSelectedIndex();
            List<Contact> queryResult = null;
            switch (queryMode) {
                case 0 : queryResult = contactController.findByName(nameInput.getText()); break;
                case 1 : queryResult = contactController.findByPhone(phoneInput.getText()); break;
                case 2 : queryResult = contactController.findByGroup((String) groupChoose.getSelectedItem()); break;
                case 3 : queryResult = contactController.findByNameAndGroup(nameInput.getText(),
                        (String) groupChoose.getSelectedItem());
            }
            DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
            tableModel.setRowCount(0);
            if(queryResult != null) {
                for (Contact contact : queryResult) {
                    String[] tableRow = {
                            contact.getName(),
                            contact.getPhone(),
                            contact.getGender(),
                            contact.getAddress(),
                            contact.getEmail(),
                            contact.getGroup(),
                            (contact.isStar()) ? "是" : ""
                    };
                    tableModel.addRow(tableRow);
                }
                table.setModel(tableModel);
                JOptionPane.showMessageDialog(null, "查询成功");
            }
            else {
                JOptionPane.showMessageDialog(null, "查询失败");
            }
            this.setVisible(false);
        }
        else if(source == reset) {
            nameInput.setText("");
            phoneInput.setText("");
            groupChoose.setSelectedIndex(0);
            queryChoose.setSelectedIndex(0);
        }
    }
}
